package mobi.weq.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import mobi.weq.dao.ObservationDao;
import mobi.weq.model.Observation;

import org.apache.commons.io.FileUtils;

public class CreateDatabase {

	public static void main(String[] args) throws IOException, ParseException,
			IllegalAccessException, IllegalArgumentException,
			InvocationTargetException, SQLException {
		ObservationDao od = new ObservationDao();
		List<String> lines = FileUtils.readLines(new File(
				"sqlScripts/edinburgh-data.csv"));

		// ignoring first since it is a title
		for (int i = 1; i < lines.size(); i++) {
			String line = lines.get(i);
			// Date,time,Carbon monoxide,PM10,Nitrogen
			// dioxide,Ozone,PM2.5,Sulphur
			// dioxide
			String[] tokens = line.split(",");
			Observation o = new Observation();

			// date and time ->
			tokens[0] = tokens[0].replaceAll("-", "/");
			String[] dTokens = tokens[0].split("/");// date
			String[] tTokens = tokens[1].split(":");// time
			Date time_stamp = new Date(Integer.parseInt(dTokens[2]) - 1900,
					Integer.parseInt(dTokens[1]) - 1,
					Integer.parseInt(dTokens[0]), Integer.parseInt(tTokens[0]),
					Integer.parseInt(tTokens[1]));
			// System.out.println(tokens[0] + "/" + time_stamp);
			o.setDatetime(time_stamp);

			// co
			try {
				double co = Double.parseDouble(tokens[2]);
				o.setCo(co);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {
				double pm10 = Double.parseDouble(tokens[3]);
				o.setPm10(pm10);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {
				double no2 = Double.parseDouble(tokens[4]);
				o.setNo2(no2);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {
				double o3 = Double.parseDouble(tokens[5]);
				o.setO3(o3);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {
				double pm2p5 = Double.parseDouble(tokens[6]);
				o.setPm2p5(pm2p5);
			} catch (Exception e) {
				// TODO: handle exception
			}

			try {
				double so2 = Double.parseDouble(tokens[7]);
				o.setSo2(so2);
			} catch (Exception e) {
				// TODO: handle exception
			}

			// Edinburgh, st leonard
			o.setLatitude(55.942485);
			o.setLongitude(-3.178955);
//			Util.printObject(o);
			od.addObservations(o);

			// System.out.println(line);
		}
		System.out.println(lines.size());
	}

}
